package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IButton;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.event.MessageEvent;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.xelavos.client.SendMessageWidget.SendMessageModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.rpc.DataService;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SendMessageController {

	private AuthenticationModel authenticationModel;
	private PrefilledTextBoxModel messageTextAreaModel;
	private SendMessageModel sendMessageModel;
	private IHTML titleHtml;

	public void registerAuthenticationModel(AuthenticationModel authenticationModel) {
		this.authenticationModel = authenticationModel;
	}

	public void registerMessageTextAreaModel(PrefilledTextBoxModel messageTextAreaModel) {
		this.messageTextAreaModel = messageTextAreaModel;
	}

	public void registerSendMessageButton(final IButton sendMessageButton) {
		sendMessageButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if(!messageTextAreaModel.isEdited()){
					EventManagerImpl.getInstance().fireEvent(new MessageEvent("escribe un mensaje"));
					return;
				}
				sendMessageButton.setEnabled(false);
				String fromUserId = authenticationModel.getUser().getId();
				String toUserId = sendMessageModel.getToUserId();
				String message = messageTextAreaModel.getText();
				EventManagerImpl.getInstance().fireEvent(new MessageEvent("enviar el mensaje...", MessageEvent.TYPE_ACTIVITY));
				DataService.Util.getInstance().sendMessage(fromUserId, toUserId, message, 
						new AsyncCallback<String>(){
					public void onFailure(Throwable arg0) {
						EventManagerImpl.getInstance().fireEvent(new MessageEvent("enviar mensaje falló"));
						sendMessageButton.setEnabled(true);
					}
					public void onSuccess(String result) {
						EventManagerImpl.getInstance().fireEvent(new MessageEvent(
								Constants.MSG_MESSAGE_SENT, MessageEvent.TYPE_INFO));
						sendMessageButton.setEnabled(true);
					}
				});
			}
		});
	}

	public void registerSendMessageModel(SendMessageModel sendMessageModel) {
		this.sendMessageModel = sendMessageModel;
		sendMessageModel.addPropertyChangeListener("toUserName", new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				updateTitleHtml();
			}
		});
	}

	public void registerSendMessageWidget(ILazyWidget sendMessageWidget) {
		sendMessageWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateTitleHtml();
			}
		});
	}

	public void registerTitleHtml(IHTML titleHtml) {
		this.titleHtml = titleHtml;
	}

	private void updateTitleHtml() {
		String toUserName = sendMessageModel.getToUserName();
		titleHtml.setVisible(toUserName != null);
		titleHtml.setHTML("Mensaje para "+toUserName);
	}
}